#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>

#define BMP280_REG_ID			0xD0

#define BMP280_CHIP_ID			0x58

static int bmp280_probe(struct i2c_client *client,
		const struct i2c_device_id *id) {
	int ret;
	u8 reg;
	u8 chip_id = 0;
	struct i2c_msg msg[2];

	/* msg[0]，第一条写消息，发送要读取的寄存器首地址 */
	msg[0].addr = client->addr; /* I2C 器件地址 */
	msg[0].flags = 0; /* 标记为发送数据 */
	reg = BMP280_REG_ID;
	msg[0].buf = &reg; /* 读取的首地址 */
	msg[0].len = 1; /* reg 长度 */

	/* msg[1]，第二条读消息，读取寄存器数据 */
	msg[1].addr = client->addr; /* I2C 器件地址 */
	msg[1].flags = I2C_M_RD; /* 标记为读取数据 */
	msg[1].buf = &chip_id; /* 读取数据缓冲区 */
	msg[1].len = 1; /* 要读取的数据长度 */

	ret = i2c_transfer(client->adapter, msg, 2);
	if (ret < 0) {
		pr_err("read BMP280_REG_ID failed\n");
		return ret;
	}
	pr_err("bmp280_probe BMP280_REG_ID  %d\n", chip_id);
	if (chip_id != 0x58) {
		dev_err(&client->dev, "bad chip id.  expected %x got %x\n",
		BMP280_CHIP_ID, chip_id);
		return -EINVAL;
	}
	return 0;
}

static const struct i2c_device_id bmp280_id[] = { { "bmp280", 0 }, { }, };
MODULE_DEVICE_TABLE(i2c, bmp280_id);

static struct i2c_driver bmp280_driver = { .driver = { .name = "bmp2801", },
		.probe = bmp280_probe, .id_table = bmp280_id, };
module_i2c_driver( bmp280_driver);

MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
MODULE_DESCRIPTION(
		"Driver for Bosch Sensortec BMP280 pressure and temperature sensor");
MODULE_LICENSE("GPL v2");
